package com.example.communitypro.util.report;

import com.example.communitypro.CommunityproApplication;
import com.example.communitypro.mapper.CommentMapper;
import com.example.communitypro.mapper.PostMapper;
import com.example.communitypro.mapper.UserInformationMapper;
import com.example.communitypro.mapper.UserReportMapper;
import com.example.communitypro.pojo.Comment;
import com.example.communitypro.pojo.Post;
import com.example.communitypro.pojo.UserInformation;
import com.example.communitypro.pojo.UserReport;
import com.example.communitypro.util.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.util.List;
import java.util.Set;

@Component("dealReport")
public class DealReport {
    @Autowired(required=false)
    private UserReportMapper userReportMapper;
    @Autowired(required = false)
    private UserInformationMapper userInformationMapper;
    @Autowired(required = false)
    private PostMapper postMapper;
    @Autowired(required = false)
    private CommentMapper commentMapper;

    public void dealReport(){
        String mgck = CommunityproApplication.class.getClassLoader().getResource("mgck").getPath().substring(1);

//        InputStream inputStream = this.getClass().getResourceAsStream("/mgck/all.xlsx");
        ClassPathResource classPathResource = new ClassPathResource("/all.txt");
        mgck = classPathResource.getPath();
        System.out.println(mgck);

        BadWordUtil2.filePath = mgck;
        BadWordUtil2.csh();
        List<UserReport> allReport = userReportMapper.findReportByReject();
        if (allReport.size()<=5){
            return;
        }
        Set<String> badWord = null;
        UserInformation userInformation = null;
        Post post = null;
        Comment comment = null;
        int tag;
        for (UserReport userReport : allReport) {
            if (userReport.getReportObjectType()==0){
                userInformation = userInformationMapper.selectByPrimaryKey(userReport.getReportObjectNo());
                badWord = BadWordUtil2.getBadWord(userInformation.getUserNickname(), 2);
                tag = 0;
            }else if (userReport.getReportObjectType()==1){
                //处理举报类型为贴子
                post = postMapper.selectByPrimaryKey(userReport.getReportObjectNo());
                badWord = BadWordUtil2.getBadWord(post.getPostContent(), 2);
                tag = 1;
            }else {
                //处理举报类型为评论
                comment = commentMapper.selectByPrimaryKey(userReport.getReportObjectNo());

                badWord = BadWordUtil2.getBadWord(comment.getCommentContent(), 2);
                tag = 2;
            }

            if (badWord.size()>0){
                if (tag==0){
                    userInformation.setUserStatus(0);
                    userInformationMapper.updateByPrimaryKeySelective(userInformation);
                }else if (tag == 1){
                    post.setPostStatus(0);
                    postMapper.updateByPrimaryKeySelective(post);
                }else {
                    comment.setCommentStatus(0);
                    commentMapper.updateByPrimaryKeySelective(comment);
                }

                StringBuilder rejectreason = new StringBuilder("违规原因[");
                for (String s : badWord) {
                    rejectreason.append("、").append(s);
                }
                rejectreason.append("]");
                userReportMapper.updateRejectReasonByReportNo(userReport.getReportNo(), rejectreason.toString());
            }else {
                userReportMapper.updateRejectReasonByReportNo(userReport.getReportNo(),"举报不成立!");
            }

        }
    }


}
